<template>
  <div>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="asideWidth" style="min-height: 100vh;background-color: #001529">
        <div
            style="height: 60px;display: flex;align-items: center;justify-content: center">
          <img src="@/assets/logo.png" style="width: 40px;height: 40px;">
          <span v-show="!isCollapse" class="logo-title">饮水系统</span>
        </div>

        <el-menu :collapse="isCollapse" :collapse-transition="false" active-text-color="#fff" background-color="#001529"
                 router
                 :default-active="$route.path"
                 style="border: none;"
                 text-color="rgba(255,255,255,0.65)">
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>
          <el-submenu index="equipment" v-if="user.role === '管理员'">
            <template v-slot:title>
              <i class="el-icon-setting"></i>
              <span>设备管理</span>
            </template>
            <el-menu-item index="/equipment">设备信息</el-menu-item>
          </el-submenu>
          <el-submenu index="user" v-if="user.role === '管理员'">
            <template v-slot:title>
              <i class="el-icon-user"></i>
              <span >用户管理</span>
            </template>
            <el-menu-item index="/user">用户信息</el-menu-item>
          </el-submenu>
          <el-menu-item index="/charts" v-if="user.role === '管理员'">
            <i class="el-icon-pie-chart"></i>
            <span slot="title">统计图表</span>
          </el-menu-item>
          <el-menu-item index="/goods">
            <i class="el-icon-goods"></i>
            <span slot="title">饮水下单</span>
          </el-menu-item>
          <el-menu-item index="/orders">
            <i class="el-icon-s-claim"></i>
            <span slot="title">订单</span>
          </el-menu-item>
          <el-menu-item index="/notice" v-if="user.role === '管理员'">
            <i class="el-icon-chat-dot-round"></i>
            <span slot="title">系统公告</span>
          </el-menu-item>
          <el-menu-item index="/logs" v-if="user.role === '管理员'">
            <i class="el-icon-info"></i>
            <span slot="title">系统日志</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-container>
        <!--头部区域-->
        <el-header>
          <i :class="collapseIcon" style="font-size: 26px;" @click="handleCollapse"></i>
          <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-left: 20px">
            <el-breadcrumb-item :to="{ path: '/' }" >主页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: $route.path }" >{{ $route.meta.name }}</el-breadcrumb-item>
          </el-breadcrumb>
          <div style="flex: 1;width: 0;display: flex;align-items: center;justify-content: flex-end">
            <i class="el-icon-quanping" style="font-size: 20px;margin-right: 20px;cursor: pointer"
               @click="handleFull"></i>
            <el-dropdown placement="bottom">
              <div style="display: flex;align-items: center;justify-content: center;cursor: default">
                <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" style="width: 40px;height: 40px;border-radius: 50%;margin-right: 10px">
                <span>{{ user.name }}</span>
              </div>

              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/person')">个人信息</el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <!--主体-->
        <el-main>
          <router-view @update:user="updateUser"/>
        </el-main>

      </el-container>
    </el-container>
  </div>
</template>

<script>

export default {
  name: 'Manager',
  data() {
    return {
      isCollapse: false,//默认不收缩
      asideWidth: "200px",
      collapseIcon: 'el-icon-s-fold',
      user: JSON.parse(localStorage.getItem('base-user') || '{}'),
    }
  },
  mounted() { //页面加载完成后触发

  },
  methods: {

    updateUser(user){ //获取子组件传过来的数据，实时更新
      this.user = JSON.parse(JSON.stringify(user))  //让父级的对象跟子级的对象毫无关联
    },
    logout() {
      localStorage.removeItem('base-user')    //清除当前token和用户数据
      this.$router.push('/login')
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse
      this.asideWidth = this.isCollapse ? "64px" : "200px"
      this.collapseIcon = this.isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'
    },
    handleFull() {
      document.documentElement.requestFullscreen()
    }
  }
}
</script>

<style>
.el-menu--inline .el-menu-item {
  background-color: #000c17 !important;
  padding-left: 49px !important;
}

.el-menu-item:hover, .el-submenu__title:hover {
  color: #fff !important;
}

.el-menu-item:hover i, .el-submenu__title:hover i {
  color: #fff !important;
}

.el-menu-item.is-active {
  background-color: #1890ff !important;
  border-radius: 5px !important;
}

.el-menu-item, .el-submenu__title {
  height: 40px;
  line-height: 40px;
}

.el-aside {
  transition: width .3s;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 35);
}

.logo-title {
  margin-left: 10px;
  padding: 10px;
  font-size: 20px;
  color: rgba(255, 255, 255, 0.65);
  transition: all 0.3s;
}

.el-header {
  box-shadow: 2px 0 6px rgba(0, 21, 41, 35);
  display: flex;
  align-items: center;
}

</style>

